package com.runda.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.model.SysOrganization;
import com.bbjob.util.Tool;
import com.bbjob.util.service.impl.BaseService;
import com.runda.system.service.OrganService;

@Service
public class OrganServiceImpl extends BaseService<SysOrganization> implements OrganService {

	@Autowired
	private SysOrganizationMapper organmapper;
	
	@Override
	public int saveOrgan(SysOrganization organ) {
		if(organ.getLevel() == 1) {
			organ.setUniqueCode(Tool.getUUID());
		}
		organ.setCreateTime(new Date());
		return organmapper.insertSelective(organ);
	}

	@Override
	public int editOrgan(SysOrganization organ) {
		// TODO Auto-generated method stub
		return organmapper.updateByPrimaryKeySelective(organ);
	}

	@Override
	public int deleteOrgan(Integer id) {
		// TODO Auto-generated method stub
		return organmapper.deleteByPrimaryKey(id);
	}
	
	public List<SysOrganization> getOrganByPid(Integer pid) {
		Example example = new Example(SysOrganization.class);
		if(pid == null) {
			example.createCriteria().andIsNull("pid");
		}else{
			example.createCriteria().andEqualTo("pid", pid);
		}
		
		return organmapper.selectByExample(example);
	}

	@Override
	public List<SysOrganization> selectTreeByPid(Integer pid) {
		List<SysOrganization> resultList = new ArrayList<SysOrganization>();
		List<SysOrganization> list = getOrganByPid(pid);
		for (SysOrganization organ : list) {
			SysOrganization entity = new SysOrganization();
			entity.setId(organ.getId());
			entity.setName(organ.getName());
			entity.setCreateTime(organ.getCreateTime());
			entity.setCreateUser(organ.getCreateUser());
			entity.setLevel(organ.getLevel());
			entity.getChildren().addAll(selectTreeByPid(organ.getId()));
			resultList.add(entity);
		}
		return resultList;
	}

	@Override
	public List<SysOrganization> getMotherTree() {
		Example example = new Example(SysOrganization.class);
		example.createCriteria().andIsNull("pid");
		return organmapper.selectByExample(example);
	}

	@Override
	public List<SysOrganization> getOrgansByPid(SysOrganization organ) {
		Example example = new Example(SysOrganization.class);
		
		if (organ.getId() == null) {
			if (organ.getUniqueCode() != null && !"".equals(organ.getUniqueCode())) {
				example.createCriteria().andEqualTo("uniqueCode", organ.getUniqueCode());
				List<SysOrganization> list = organmapper.selectByExample(example);
				
				if (list.size() > 0) {
					SysOrganization item = list.get(0);
					example.clear();
					
					example.createCriteria().andEqualTo("pid", item.getId());
					
					return organmapper.selectByExample(example);
				} 
			} else {
				
				example.createCriteria().andIsNull("pid");
				return organmapper.selectByExample(example);
			}
		} else {
			
			example.createCriteria().andEqualTo("pid", organ.getId());
			
			return organmapper.selectByExample(example);
		}
		
		return null;
	}

	@Override
	public SysOrganization getOrganInfo(String uniquecode) {
		if (uniquecode != null && !"".equals(uniquecode)) {
			Example example = new Example(SysOrganization.class);
			example.createCriteria().andEqualTo("uniqueCode", uniquecode);
			List<SysOrganization> list = organmapper.selectByExample(example);
			if (list.size() > 0) {
				
				return list.get(0);
			} 
		} 
		
		return null;
	}
}